home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / allison / destroy2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-02  |  642 b   |  52 lines

  1. LISTING 2 - Illustrates Stack Unwinding
  2.  
  3. // destroy2.cpp
  4. #include <iostream.h>
  5.  
  6. class Foo
  7. {
  8. public:
  9.     Foo() {cout << "Foo constructor" << endl;}
  10.     ~Foo() {cout << "Foo destructor" << endl;}
  11. };
  12.  
  13. void f();
  14. void g();
  15.  
  16. main()
  17. {
  18.     try         // Turn on exception handling
  19.     {
  20.         f();
  21.     }
  22.  
  23.  
  24.     catch(...)  // Ellipsis matches any exception
  25.     {
  26.         cout << "Caught exception" << endl;
  27.     }
  28.     return 0;
  29. }
  30.  
  31. void f()
  32. {
  33.     Foo x;
  34.  
  35.     g();
  36. }
  37.  
  38. void g()
  39. {
  40.     Foo x;
  41.  
  42.     throw 1;
  43. }
  44.  
  45. /* Output:
  46. Foo constructor
  47. Foo constructor
  48. Foo destructor
  49. Foo destructor
  50. Caught exception
  51. */
  52.